package com.example.prjbackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.prjbackend.entity.Department;
import com.example.prjbackend.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /***
     * 新增部门
     * @param department 以json形式传输的部门
     * @return 结果
     */
    @PostMapping("/register")
    public Object register(@RequestBody Department department) {
        boolean result = departmentService.dept_register(department);
        return result ? "部门登记成功" : "部门登记失败";
    }

    /***
     *  部门更新
     * @param department 以json形式传输的部门
     * @return 部门更新结果
     */
    @PostMapping("/update")
    public Object update(@RequestBody Department department) {
        boolean result = departmentService.dept_update(department.getDept_name(), department.getDept_desc());
        return result ? "部门修改成功" : "部门修改失败";
    }


    @GetMapping("/find")
    public Page<Department> listDepts(
            @RequestParam(required = false) String deptName,
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "3") Long size) {
        return departmentService.findByDeptname(deptName, current, size);
    }


    /**
     * 测试接口
     * @return 测试消息
     */
    @GetMapping("/test")
    public String test() {
        return "后端服务运行正常！";
    }



}
